package com.data.controller;

import com.sky.result.Result;
import com.data.pojo.vo.OrderReportVO;
import com.data.pojo.vo.SalesTop10ReportVO;
import com.data.pojo.vo.TurnoverReportVO;
import com.data.pojo.vo.UserReportVO;
import com.data.service.ReportService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;

@RestController
@Slf4j
@RequestMapping("/admin/report")
@Api(tags = "营业额统计接口")
public class ReportController {
    @Autowired
    ReportService reportService;

    /**
     * 统计营业额
     * @param begin
     * @param end
     * @return
     */
    @GetMapping("turnoverStatistics")
    @ApiOperation("统计营业额")
    public Result<TurnoverReportVO> turnoverStatistics(
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("begin:{},end:{}", begin, end);
        TurnoverReportVO turnoverReportVO=reportService.turnoverStatistics(begin,end);
        return Result.success(turnoverReportVO);
    }

    /**
     * 统计用户数量
     * @param begin
     * @param end
     * @return
     */
    @GetMapping("userStatistics")
    @ApiOperation("统计用户数量")
    public Result<UserReportVO> userStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                               @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("begin:{},end:{}", begin, end);
        UserReportVO userReportVO=reportService.userStatistics(begin,end);
        return Result.success(userReportVO);
    }

    /**
     * 统计订单数量
     * @return
     */
    @GetMapping("ordersStatistics")
    @ApiOperation("统计订单数量")
    public Result<OrderReportVO> orderStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                                 @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("begin:{},end:{}", begin, end);
        OrderReportVO orderReportVO=reportService.orderStatistics(begin,end);
        return Result.success(orderReportVO);
    }

    /**
     * 统计销量Top10
     * @param begin
     * @param end
     * @return
     */
    @GetMapping("top10")
    @ApiOperation("销量top10")
    public Result<SalesTop10ReportVO> top10Statistics(@RequestParam("begin") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,  // 添加@RequestParam
                                                      @RequestParam("end") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end  ){
        log.info("begin:{},end:{}", begin, end);
        SalesTop10ReportVO salesTop10ReportVO=reportService.top10Statistics(begin,end);
        return Result.success(salesTop10ReportVO);

    }

    /**
     * 导出Excel报表
     * @param response
     */
    @GetMapping("export")
    @ApiOperation("导出Excel报表接口")
    public void export(HttpServletResponse response) throws IOException {
        reportService.export(response);
    }


}
